package org.xx.sipahi.jpa;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.xx.sipahi.Category;

import java.util.List;

public interface CategoryRepository
        extends JpaRepository<CategoryPO, Integer>, JpaSpecificationExecutor<CategoryPO> {
    int NOTICE = 1;
    int PRESENTATION = 2;

    static Category toCategory(
            CategoryPO po
    ) {
        if (po == null) {
            return Category.EMPTY;
        } else {
            return new Category(po.getId(), po.getTitle(), po.getDescription(), po.getPageSize(),
                                po.getAuthority(), po.getModule(), po.isEnabled(), po.getTpl1(), po.getTpl2());
        }
    }

    default CategoryPO getOneOrNull(
            Integer id
    ) {
        return id != null ? this.getOne(id) : null;
    }

    @Query("select id, title from category where module = :module order by id")
    List<Object[]> findAllKeyValue(
            int module
    );
}
